#include "SpringRoot.h"
#include "Vec2d.h"
#include <cmath>

SpringRoot::SpringRoot()
{
	k = 0.0;
	length = 0.0;
}

SpringRoot::SpringRoot(float k, float length)
{
	this->k = k;
	this->length = length;
}

void SpringRoot::affect(Entity *ent)
{
	Vec2d disp = ent->pos - pos; // displacement vector from springRoot to entity
	float force = (-k) * (sqrt(disp * disp) - length);
	ent->accel = ent->accel + disp.normalize() * force * ent->getInvMass(); // disp.normalize() finds direction from spring to entity
}

void SpringRoot::customAffect(Entity *ent, float k, float length)
{
	Vec2d disp = ent->pos - pos; // displacement vector from springRoot to entity
	float force = (-k) * (sqrt(disp * disp) - length);
	ent->accel = ent->accel + disp.normalize() * force * ent->getInvMass(); // disp.normalize() finds direction from spring to entity
}